package com.fuhe.chen.vendingmachine.pojo;

import com.fuhe.chen.vendingmachine.constant.WebConst;
import lombok.Data;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 操作日志
 */
@Data
public class Log {

    /**
     * 主键
     */
    private Integer id;

    /**
     * 用户主键
     */
    private String user_id;

    /**
     * 操作
     */
    private String operation;

    /**
     * 操作时间
     */
    private Long operate_date;

    private Log(String user_id, String operation, Long operate_date) {
        this.user_id = user_id;
        this.operation = operation;
        this.operate_date = operate_date;
    }

    public static Log logging(String operation){
        Subject subject = SecurityUtils.getSubject();
        User userInfo = (User) subject.getSession().getAttribute(WebConst.LOGIN_SESSION_KEY);
        long currentTimeMillis = System.currentTimeMillis();
        String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentTimeMillis);
        if(userInfo==null){
            return new Log(null,"未登录用户,做出了 "+operation+"操作,时间为:"+currentTime, currentTimeMillis);
        }else{
            String operationMessage = "用户 "+userInfo.getName()+" ,做出了 "+operation+" 操作,时间为:"+currentTime;
            return new Log(userInfo.getId(),operationMessage, currentTimeMillis);
        }
    }

}
